﻿var lastReminderElem = null;
var lastReminderElemBg = null;
var reminderOperation = "";

function loadLastReminderElements() 
{
    var elem = document.getElementById(lastReminderElem);
    var elemChild = elem.getElementsByTagName("td");
    var alarmDate = elemChild[2].innerHTML;
    
    var dateExplode = alarmDate.split('/');
    var month = dateExplode[0];
    var day = dateExplode[1];
    var yearTime = dateExplode[2];

    var yearTimeExplode = yearTime.split(' ');
    var year = yearTimeExplode[0];
    var time = yearTimeExplode[1];
    var AMPM = yearTimeExplode[2];

    var timeExplode = time.split(':');
    var hour = timeExplode[0];
    var min = timeExplode[1];
    var sec = timeExplode[2];
    
    document.getElementById('txtTitle').value = elemChild[0].innerHTML;
    document.getElementById('txtDescription').value = elemChild[1].innerHTML;
    document.getElementById('txtAlarmMonth').value = month;
    document.getElementById('txtAlarmDate').value = day;
    document.getElementById('txtAlarmYear').value = year;
    document.getElementById('txtAlarmHour').value = hour;
    document.getElementById('txtAlarmMin').value = min;
    //document.getElementById('txtAlarmSec').value = sec;
    document.getElementById('txtAMPM').value = AMPM;
}

function selectReminderTableElement(elemID) 
{
    if (lastReminderElem != null) {
        document.getElementById(lastReminderElem).style.backgroundColor = lastReminderElemBg;
    }
    var elem = document.getElementById(elemID);
    lastReminderElem = elemID;
    lastReminderElemBg = elem.style.backgroundColor;
    elem.style.backgroundColor = "#FEF0C9";
}

function showReminderPopup(operation) 
{
    if (operation == "Add") {
        document.getElementById('btnReminderOperation').value = "Add Reminder";
        document.getElementById('tdOperation').innerHTML = "Add Reminder";
        clearReminderPopUpElements();
    }
    else
        if (operation == "Edit") {
        if (lastReminderElem == null)
            alert('No Reminder Selected');
        loadLastReminderElements();
        document.getElementById('btnReminderOperation').value = "Save Changes";
        document.getElementById('tdOperation').innerHTML = "Edit Reminder";
    }
    document.getElementById('lblErrorMessage').innerHTML = '';
    document.getElementById('divReminderPopup').style.display = '';
    relocateReminderPopup();
    document.getElementById('divReminders').style.opacity = 0.5;
    reminderOperation = operation;

    toggleReminderButtons(false);
}

function clearReminderPopUpElements() 
{
    document.getElementById('txtTitle').value = '';
    document.getElementById('txtDescription').value = '';

    dt = new Date();
    document.getElementById('txtAlarmMonth').value = dt.getMonth() + 1;
    document.getElementById('txtAlarmDate').value = dt.getDate();
    document.getElementById('txtAlarmYear').value = dt.getFullYear();

    hour = dt.getHours();
    document.getElementById('txtAlarmHour').value = hour > 12 ? (hour-12):hour;
    document.getElementById('txtAlarmMin').value = dt.getMinutes();    
    document.getElementById('txtAMPM').value = hour > 12 ? 2:1;
}

function relocateReminderPopup() 
{
    var popup = document.getElementById("divReminderPopup")
    if (popup.style.display != 'none') {
        var width = getViewPortWidth();
        width -= 500;
        width -= popup.style.width.replace("px", "");
        popup.style.left = width / 2 + 'px';
        popup.style.top = "100px";
    }
}

function toggleReminderButtons(enable) 
{
    document.getElementById('btnAddReminder').disabled = !enable;
    document.getElementById('btnEditReminder').disabled = !enable;
    document.getElementById('btnDeleteReminder').disabled = !enable;
}

function CancelReminderOperation() 
{
    document.getElementById('divReminderPopup').style.display = 'none';
    document.getElementById('divReminders').style.opacity = 1.0;
    toggleReminderButtons(true);
}

function PerformReminderOperation() 
{
    var title = document.getElementById('txtTitle').value;
    var desc = document.getElementById('txtDescription').value;
    var userName = document.getElementById('txtCurrentUser').value;
    var month = document.getElementById('txtAlarmMonth').value;
    var date = document.getElementById('txtAlarmDate').value;
    var year = document.getElementById('txtAlarmYear').value;
    var hour = document.getElementById('txtAlarmHour').value;
    var min = document.getElementById('txtAlarmMin').value;
    //var sec = document.getElementById('txtAlarmSec').value;
    var ampm = document.getElementById('txtAMPM').value;
    
    var alarmDate = month + '/' + date + '/' + year + ' ' + hour + ':' + min + ':' + "00" + ' ' + ampm;
    
    //check required fields
    errMsg = "";
    if (title == "") 
        errMsg += "Title is required.</br>";
    
    if (year == "")
        errMsg += "Year is required.</br>";

    //check if alarm time is still in the future
    currentDate = new Date();
    dt_alarmDate = new Date(alarmDate);

    currentDate.setSeconds(0,0);

    //alert('Current Date: ' + currentDate + '\nAlarm Date: ' + dt_alarmDate);

    if (dt_alarmDate < currentDate)
        errMsg += "Alarm date/time must be greater than current date/time.</br>";
    
    //check invalid dates
    if (month == 2) {
        if(date == 30)
            errMsg += "Selected month does not have day 30.</br>";
        if (date == 29) {
            if((year - 2000) % 4 != 0)
                errMsg += "February can only have day 29 on leap years.</br>";
        }  
    }
     
    if((month == 2 ||month == 4 || month == 6 || month == 9 || month == 11) &&
        date == 31)
        errMsg += "Selected month does not have day 31.</br>";

    if (errMsg != "") {
        document.getElementById('lblErrorMessage').innerHTML = errMsg;
        return;
    }

    var url = "Personal/Reminders.aspx";

    if (reminderOperation == "Add") {
        url = url + "?insertreminder=1";
        url = url + "&username=" + URLEncode(userName);
    }
    else
        if (reminderOperation == "Edit") {
        url = url + "?updatereminder=1";
        try {
            reminderID = document.getElementById(lastReminderElem).getAttribute("reminderID");
        }
        catch (ex) {
            alert('No Reminder Selected');
            return;
        }
        url = url + "&reminderID=" + URLEncode(reminderID);
    }


    url = url + "&title=" + URLEncode(title);
    url = url + "&desc=" + URLEncode(desc);
    url = url + "&alarmDate=" + URLEncode(alarmDate);
    url = url + "&sid=" + Math.random();

    //alert(url);
    response = SubmitReminderForm(url);
    //alert(response);
    if (response.indexOf("Success") > -1) {
        loadXMLDoc('Personal/Reminders.aspx');
        if (lastReminderElem != null) {
            document.getElementById(lastReminderElem).style.backgroundColor = "#FEF0C9";
        }
        if (reminderOperation == "Add") {
            alert("New Reminder Successfully Added");
        }
        else if (reminderOperation == "Edit") {
            alert("Reminder Successfully Updated");
        }
    }
    else {
        document.getElementById('lblErrorMessage').innerHTML = response;
        // alert(response);
    }

    return false;
}

function deleteSelectedReminder() 
{
    if (lastReminderElem == null) {
        alert('No Reminder Selected');
    }
    else {
        var result = confirm("Are you sure you want to delete this reminder?");

        if (result == 1) {
            var reminderID = document.getElementById(lastReminderElem).getAttribute("reminderID");
            var url = "Personal/Reminders.aspx";
            url = url + "?deletereminder=1";
            url = url + "&reminderID=" + URLEncode(reminderID);
            url = url + "&sid=" + Math.random();

            //alert(url);

            response = SubmitReminderForm(url);

            if (response.indexOf("Success") > -1) {
                loadXMLDoc('Personal/Reminders.aspx');
                alert("Reminder Successfully Deleted");
            }
            else {
                alert("There was a problem deleting the reminder");
            }
        }
    }
}

function SubmitReminderForm(url) 
{
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    response = xmlhttp.responseText;
    //alert(response);
    //alert(response.substring(response.indexOf("Line"),response.length));

    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));
    //response = response.substring(response.indexOf("Source Error"));
    //alert(response);

    return response;
}



